<template>
  <view>
    <!-- 使用搜索框自定义组件 -->
    <my-search @click="gotoSearch"></my-search>
    <view class="scroll-view-containner">
      <!-- 左侧scroll-view组件 -->
      <scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh+'px'}">
        <view v-for="(item,i) in cateList" :key="i">
          <view :class="['left-scroll-view-item',i==active?'active':'']" @tap="activeChange(i)">
            {{item.cat_name}}
          </view>
        </view>
      </scroll-view>
      <!-- 右侧scroll-view组件 -->
      <scroll-view class="right-scroll-view" scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <!-- 二级分类 -->
        <view v-for="(item,i) in cateLevel2" :key="i">
          <!-- 二级分类标题 -->
          <view class="level2-title">/ {{item.cat_name}} /</view>
          <!-- 二级分类子项 -->
          <view class="level2-bottom">
            <view class="level2-item" @tap="gotoGoodsList(item1)" v-for="(item1,i1) in item.children" :key='i1'>
                <image :src="item1.cat_icon" style="width: 60px;" mode="widthFix"></image><br>
                {{item1.cat_name}}
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
    data() {
      return {
        wh:0,
        cateList:[],
        active:0,
        cateLevel2:[],
        scrollTop:0,
      };
    },
    methods:{
      // 请求分类数据
      async _getCateList(){
        const {data:res}=await uni.$http.get('/api/public/v1/categories')
        if(res.meta.status!==200)return uni.$showMsg()
        this.cateList=res.message
        this.cateLevel2=res.message[0].children
        // console.log(res)
      },
      // 左侧滑动模块选择项点击事件
      activeChange(val){
        this.active=val
        this.cateLevel2=this.cateList[val].children
        this.scrollTop=this.scrollTop==1?0:1
      },
      // 右侧滑动模块最小子项点击跳转事件
      gotoGoodsList(val){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+val.cat_id
        })
      },
      // 跳转到search页面
      gotoSearch(){
        uni.navigateTo({
          url:'../../subpkg/search/search'
        })
      }
    },
    onLoad(){
      this._getCateList()
      const sysInfo=uni.getSystemInfoSync()
      this.wh=sysInfo.windowHeight-50
    }
  }
</script>

<style lang="scss">
.scroll-view-containner{
  display: flex;
  // 左侧滑动组件
  .left-scroll-view{
    width: 120px;
    .left-scroll-view-item{
      position: relative;
      line-height:50px;
      text-align: center;
      background-color: #f7f7f7;
      font-size: 14px;
      &.active{
        background-color: #fff;
        &::before{
          position: absolute;
          content: '';
          display: block;
          width: 3px;
          height: 50rpx;
          background-color: #f00000;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }
}
  // 右侧滑动组件
  .right-scroll-view{
    flex: 1;
    .level2-title{
      line-height: 40px;
      text-align: center;
      font-size: 14px;
      font-weight: 600;
    }
    .level2-bottom{
      display: flex;
      flex-wrap: wrap;
      .level2-item{
        width: 33.33%;
        text-align: center;
        font-size: 12px;
        margin-bottom: 25px;
      }
    }
  }
</style>
